package cn.ling.mini.spring.context;

import cn.ling.mini.spring.beans.factory.config.BeanDefinition;
import cn.ling.mini.spring.beans.factory.BeanFactory;
import cn.ling.mini.spring.beans.factory.SimpleBeanFactory;
import cn.ling.mini.spring.beans.factory.xml.XmlBeanDefinitionReader;
import cn.ling.mini.spring.core.ClassPathXmlResource;
import cn.ling.mini.spring.exception.BeanException;

/**
 * @author LING
 * @date 2024-10-10
 */
public class ClassPathXmlApplicationContext {

    private BeanFactory beanFactory;
    public ClassPathXmlApplicationContext(String fileName) {
        ClassPathXmlResource resource = new ClassPathXmlResource(fileName);
        SimpleBeanFactory beanFactory = new SimpleBeanFactory();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
        reader.loadBeanDefinitions(resource);
        this.beanFactory = beanFactory;
    }

    public Object getBean(String beanName) throws BeanException {
        return beanFactory.getBean(beanName);
    }

    public void registerBeanDefinition(BeanDefinition beanDefinition) {
        this.beanFactory.registerBeanDefinition(beanDefinition);
    }

}
